package com.yuhan.common.utils;


import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 分页
 * @author: hejing
 * @create: 2019/8/29
 **/
@Getter
@Setter
@NoArgsConstructor
public class Page {

    /**
     * 页码
     */
    public int pageNumber = 1;

    /**
     * 每页数据
     */
    public int pageSize = 20;

    /**
     * 返回结果
     */
    public List<Object> content = new ArrayList<>();

    /**
     * 数据总数
     */
    public long total = 0;

    public Page(int pageNumber, int pageSize) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
    }

    /**
     * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
     */
    public int getFirst() {
        return (this.total == 0) ? 0 : (pageNumber - 1) * pageSize;
    }


    public long getTotalPages() {
        if (total < 0) {
            return 0;
        }
        long count = total / pageSize;
        if (total % pageSize > 0) {
            count++;
        }
        return count;
    }

}
